package com.ikas.ai.server.module.system.controller;

import com.github.pagehelper.PageInfo;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.handle.JsonResultType;
import com.ikas.ai.server.aop.ColumnCheck;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.system.model.Log;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.server.module.system.service.LogService;
import com.ikas.ai.utils.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统管理-日志管理
 * @author hxh
 */
@RestController
@RequestMapping("/system/log")
public class LogController extends AbstractBean {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private static final String COLUMN_NAMES = "id,createTime";
    @Autowired
    private LogService logService;

    /**
     * 查询日志类型
     * @return
     */
    @GetMapping(value = "/opstypes")
    @ResponseBody
    public String getLogTypes() {
        Map<String, String> map = new LinkedHashMap<>();
        for (LogOpsType type : LogOpsType.values()) {
            map.put(String.valueOf(type.getIndex()), type.getName());
        }
        JsonResult result = new JsonResult(JsonResultType.OK);
        result.add(map);
        return result.toJSON();
    }

    /**
     * 分页获取日志
     * @param request
     * @param userName
     * @param opsType
     * @param dateRange
     * @param pageNo
     * @param pageSize
     * @param sortColumnName
     * @param sortOrder
     * @return
     */
    @GetMapping(value = "/list")
    public String pageQueryLogs(HttpServletRequest request, @RequestParam(value = "userName", required = false) String userName,
                                @RequestParam(value = "opsType", required = false) String opsType,
                                @RequestParam(value = "dateRange", required = false) String dateRange,
                                @RequestParam(value = "page", required = false) Integer pageNo,
                                @RequestParam(value = "size", required = false) Integer pageSize,
                                @RequestParam(value = "sc", required = false) @ColumnCheck(value = COLUMN_NAMES) String sortColumnName,
                                @RequestParam(value = "so", required = false) String sortOrder) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(dateRange)) {
            String[] ss = dateRange.split(",");
            if (ss.length > 0) {
                startTime = ss[0];
            }
            if (ss.length > 1) {
                endTime = ss[1];
            }
        }
        JsonResult result = new JsonResult(JsonResultType.OK);
        PageUtil.configPageHelper(pageNo, pageSize, sortColumnName, sortOrder, "createTime", "desc");
        List<Log> logs = logService.getLogs(userName, opsType, startTime, endTime);
        logs.forEach(o->o.setTypeName(LogOpsType.getName(o.getType())));
        PageInfo<Log> pageInfo = new PageInfo<>(logs);
        result.add(pageInfo);
        return result.toJSON();
    }
}
